/**
 * @overview 创建圆
 * @author dengqiang
 * @description 原生Cesium
 * @update  2022.11.15
 */
class GWarCircle {
  constructor() {
    window.GWarCircleArray = []
  }

  //新增单个圆
  add(paramObj) {
    const { name, option, parentName } = paramObj

    const position = window.cesiumMapTool.car3ToWGS84Two(option.position)

    const centerPos = window.cesiumMapTool.WGS84ToCar3(position)

    //计算圆的线段点数
    const turfOptions = { steps: option.circle.radius * 4 }
    const { geometry } = turf.circle(
      [position.lng, position.lat],
      option.circle.radius * 0.001,
      turfOptions
    )

    geometry.coordinates[0].forEach((item) => {
      item.push(position.alt)
    })
    const polylineArr = geometry.coordinates[0].map((item) => {
      return Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2])
    })

    //原生方式
    const entity = {
      position: centerPos,

      label: {
        text: option.label.text || '',
        font: option.label.font || '18px san-serif',
        pixelOffset: option.label.labelOffset || new Cesium.Cartesian2(0, -10),
        distanceDisplayCondition:
          option.label.distanceDisplayCondition || new Cesium.DistanceDisplayCondition(0, 6000),
        disableDepthTestDistance: 10000,
        showBackground: true, //是否显示背景
        backgroundColor: option.label.backgroundColor || new Cesium.Color(0.165, 0.165, 0.165, 0.8), //背景颜色
        backgroundPadding: option.label.backgroundPadding || new Cesium.Cartesian2(7, 5), //背景边距
        fillColor: option.label.fillColor || Cesium.Color.WHITE, //填充色
        outlineColor: option.label.outlineColor || new Cesium.Color(0, 1, 1, 0), //外线颜色
        outlineWidth: option.label.outlineWidth || 1.0, //外线宽
      },

      polyline: {
        positions: polylineArr,
        width: 5,
        material: Cesium.Color.RED.withAlpha(1),
      },

      ellipse: {
        height: position.alt,
        semiMinorAxis: option.circle.radius || 5, //椭圆短轴（单位米）
        semiMajorAxis: option.circle.radius || 5, //椭圆长轴（单位米）
        material:
          (option.circle.fillColor && Cesium.Color.fromCssColorString(option.circle.fillColor)) ||
          Cesium.Color.WHITE.withAlpha(0.01),
      },
    }

    const circle = window.map3DControl.viewer.entities.add(entity)
    circle.attribute = option.attribute || {} //支持添加自定义属性
    circle.attribute.name = name
    circle.attribute.parentName = parentName
    circle.attribute.position = position
    circle.attribute.radius = option.circle.radius

    const circleObject = {
      name: name,
      parentName: parentName,
      circle: circle,
    }
    window.GWarCircleArray.push(circleObject)
    return circle
  }

  //根据name移除单个面或体
  removeByName(name) {
    window.GWarCircleArray.forEach((item, index) => {
      if (item.name == name) {
        window.GWarCircleArray.splice(index, 1)
        window.map3DControl.viewer.entities.remove(item.circle)
      }
    })
  }

  //根据parentName移除一批面或体
  removeByPatName(parentName) {
    const length = window.GWarCircleArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GWarCircleArray[i].parentName == parentName) {
        window.map3DControl.viewer.entities.remove(window.GWarCircleArray[i].circle)
        window.GWarCircleArray.splice(i, 1)
      }
    }
  }

  //移除所有面或体
  removeAll() {
    window.GWarCircleArray.forEach((item) => {
      window.map3DControl.viewer.entities.remove(item.circle)
    })
    window.GWarCircleArray = []
  }

  //根据name控制单个面或体的显隐
  setVisibleByName(name, boolean) {
    window.GWarCircleArray.forEach((item) => {
      if (item.name == name) {
        item.circle.show = boolean
      }
    })
  }

  //根据parentName控制一批面或体的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GWarCircleArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.circle.show = boolean
      }
    })
  }

  //根据name修改标注名称
  setLabelText(name, text) {
    window.GWarCircleArray.forEach((item) => {
      if (item.name === name) {
        item.circle.label.text.setValue(text)
      }
    })
  }

  //根据name设置圆的位置
  setPosition(name, newPosition) {
    window.GWarCircleArray.forEach((item) => {
      if (item.name === name) {
        const position = window.cesiumMapTool.car3ToWGS84Two(newPosition)

        const radius = item.circle.attribute.radius

        //计算圆的线段点数
        const turfOptions = { steps: radius * 4 }
        const { geometry } = turf.circle([position.lng, position.lat], radius * 0.001, turfOptions)
        geometry.coordinates[0].forEach((item) => {
          item.push(position.alt)
        })
        const polylineArr = geometry.coordinates[0].map((item) => {
          return Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2])
        })

        item.circle.ellipse.height = position.alt
        item.circle.position = newPosition
        item.circle.polyline.positions = polylineArr
      }
    })
  }

  //根据name设置选中效果
  setActive(name, boolean, time = 100) {
    window.GWarCircleArray.forEach((item) => {
      if (item.name == name) {
        //如果圆没有填充色 线添加选中效果
        if (item.circle.ellipse.material.color.getValue().alpha === 0.01) {
          window.cesiumMapTool.requestAnimationFrameTwo(
            item.circle.polyline.material.color,
            boolean,
            200,
            item.circle
          )
        } else {
          window.cesiumMapTool.requestAnimationFrameTwo(
            item.circle.ellipse.material.color,
            boolean,
            time,
            item.circle
          )
        }
      }
    })
  }

  //移除全部选中效果
  removeActive(time = 100) {
    window.GWarCircleArray.forEach((item) => {
      window.cesiumMapTool.requestAnimationFrameTwo(
        item.circle.ellipse.material.color,
        false,
        time,
        item.circle
      )
    })
  }
}

export default GWarCircle
